# -*- coding: utf-8 -*-
# created on 2016/10/10
from mathsolver.functions.zhixian.base import default_symbols
from mathsolver.functions.base import *
from sympy import simplify, solveset
from sympy.abc import t, x, y, a
from mathsolver.functions.root.jiefangchen import JieFangChen


class QuXianDaoShu(BaseFunction):
    def solver(self, *args):
        eq = args[0].sympify()
        f = eq[0] - eq[1]
        aa, h = f.as_independent(y)
        left = h
        right = -(f - left)
        f_name = left / left.coeff(y)
        expression = right / left.coeff(y)
        # 推导步骤
        if expression.is_Add:
            # 处理减号的问题（减号被当做加号处理）
            intermediate = "+".join("({})'".format(new_latex(argument)) for argument in
                                    expression.args).replace(r"+(-", "-(")
        elif expression.is_Mul:
            first_part = "".join("{}".format(new_latex(argument)) for argument in expression.args[:-1])
            second_part = new_latex(expression.args[-1])
            intermediate = "({0})'{1} + {0}({1})'".format(first_part, second_part)
        else:
            intermediate = None
        # 结果
        result = simplify(expression.diff(x), ratio=1)
        # 第 1234 个步骤
        steps12 = "{0}'({1}) = ({2})'".format(f_name, x, new_latex(expression))
        # 如果不是复合函数的话，省去第3步
        steps34 = " = {}".format(new_latex(result)) if intermediate is None \
            else " = {0} = {1}".format(intermediate, new_latex(result))
        self.steps.append([steps12, steps34])
        self.output.append(BaseEq([f_name, result]))
        self.label.add("求曲线的导数")
        return self


class QieXianQieDian(BaseFunction):
    def solver(self, *args):
        eq = args[0].sympify()
        expr = eq[0] - eq[1]
        symbol = default_symbol(expr)
        new_expr = expr.subs({symbol: t})
        ans = list(solveset(new_expr, y))[0]
        self.steps.append(["", "设切点P为(%s, %s)" % (new_latex(t), new_latex(ans))])
        self.output.append(BasePoint({"name": "P", "value": [t, ans]}))
        return self


class QieDianXieLv(BaseFunction):
    def solver(self, *args):
        point_name = args[0].name
        point_value = args[0].sympify()
        eq = args[1].sympify()
        symbol = default_symbol(eq[1])
        qiexianxielv = eq[1].subs({symbol: point_value[0]})
        self.steps.append(["", "∴曲线在%s(%s,%s)的切线斜率为 %s" % (new_latex(point_name), new_latex(point_value[0]), new_latex(point_value[1]), new_latex(qiexianxielv))])
        self.output.append(BaseValue(qiexianxielv))
        return self


class JieFangChen010(BaseFunction):
    def solver(self, *args):
        eq = args[0].sympify()
        stepsolver = JieFangChen().solver(BaseEq(eq))
        self.steps += stepsolver.steps
        self.label.update(stepsolver.label)
        self.output.append(stepsolver.output[0])
        return self


class QieXianFangCheng003(BaseFunction):
    def solver(self, *args):
        eq = args[0].sympify()
        symbol = default_symbol(eq[0] - eq[1])
        line_name = args[1].sympify()
        expr = eq[0] - eq[1]
        if expr.subs({x: line_name[0], y: line_name[1]}) == 0:
            qiedian = line_name
        else:
            qiedian = [a, eq[1].subs({symbol: a})]
        self.steps.append(["", "设切点P(%s,%s)" % (new_latex(qiedian[0]), new_latex(qiedian[1]))])
        stepsolver = QuXianDaoShu().solver(args[0])
        self.steps += stepsolver.steps
        daoshu = stepsolver.output[0].sympify()
        xielv = daoshu[1].subs({symbol: qiedian[0]})
        self.steps.append(["", "∴ %s = %s在点P(%s,%s)处的导数为%s" % (new_latex(eq[0]), new_latex(eq[1]), new_latex(qiedian[0]), new_latex(qiedian[1]), new_latex(xielv))])
        self.steps.append(["∴切线方程为", BaseZhiXian({"name": line_name, "value": [y - qiedian[1], xielv * (x - qiedian[0])]}).printing()])
        self.output.append(BaseZhiXian({"name": line_name, "value": [y - qiedian[1], xielv * (x - qiedian[0])]}))
        return self


if __name__ == '__main__':
    pass
